#include "TKSPStdAfx.h"

#include "DBXDataParser.h"

#include <libconfig.hpp>

namespace DBX
{
    namespace Constants
    {
        extern const char *keyDataLabel = "label";
        extern const char *keyDataType = "type";
        extern const char *keyDataValue = "value";
    } // namespace Constants
    class DBXDataParser::Private
    {
    public:
        Private(DBXDataParser *p)
            : o(p)
        {}

        Setting &create(const std::string &key)
        {
            auto &root = cfg.getRoot();
            if(!root.exists(key)) {
                root.add(key, Setting::TypeGroup);
            }
            return root[key];
        }

        void setLabel(Setting &setting, const std::string &label)
        {
            if(!setting.exists(Constants::keyDataLabel)) {
                setting.add(Constants::keyDataLabel, Setting::TypeString);
            }
            setting[Constants::keyDataLabel] = label;
        }

        void setType(Setting &setting, int type)
        {
            if(!setting.exists(Constants::keyDataType)) {
                setting.add(Constants::keyDataType, Setting::TypeInt);
            }
            setting[Constants::keyDataType] = type;
        }

        Setting &getValue(Setting &setting) const
        {
            if(!setting.exists(Constants::keyDataValue)) {
                setting.add(Constants::keyDataValue, Setting::TypeGroup);
            }
            return setting[Constants::keyDataValue];
        }

    private:
        friend class DBXDataParser;
        DBXDataParser *o;
        Config cfg;
    };

    DBXDataParser::DBXDataParser()
        : p(*new Private(this))
    {}

    DBXDataParser::~DBXDataParser()
    {
        delete &p;
    }

    Setting &DBXDataParser::getRoot() const
    {
        return p.cfg.getRoot();
    }

    Setting &DBXDataParser::create(const std::string &key)
    {
        return p.create(key);
    }

    void DBXDataParser::setLabel(Setting &setting, const std::string &label)
    {
        p.setLabel(setting, label);
    }

    void DBXDataParser::setType(Setting &setting, int type)
    {
        p.setType(setting, type);
    }

    Setting &DBXDataParser::getValue(Setting &setting) const
    {
        return p.getValue(setting);
    }
} // namespace DBX